package com.jinlejia.bbs.adapter;

import android.content.Context;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jinlejia.bbs.R;

import java.util.Collection;

/**
 * @author 王进 on 2019/4/3.
 */

public abstract class BaseLoadAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    protected final Context mContext;
    protected T mData;

    protected final int TYPE_LOAD = 999;
    protected final int TYPE_EMPTY = 998;
    private int tempType;

    public BaseLoadAdapter(Context context) {
        this.mContext = context;
        this.tempType = TYPE_LOAD;
    }

    public final void initData(T data){
        if (mData==null)
            mData = data;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        if (viewType == TYPE_LOAD) {
            tempType = -1;
            return new LoadViewHolder(inflater.inflate(R.layout.bbs_load_layout, parent, false));
        } else if (viewType == TYPE_EMPTY) {
            return new EmptyViewHolder(inflater.inflate(R.layout.bbs_empty_layout, parent, false));
        }
        return onCreateViewHolder(inflater, parent, viewType);
    }

    protected abstract RecyclerView.ViewHolder onCreateViewHolder(LayoutInflater inflater, ViewGroup parent, int viewType);



    @Override
    public final int getItemViewType(int position) {
        if (tempType == TYPE_LOAD) {
            return TYPE_LOAD;
        } else {
            if (mData == null) {
                return TYPE_LOAD;
            } else if (mData instanceof Collection) {
                if (((Collection) mData).size() == 0) {
                    return TYPE_EMPTY;
                }
            }

        }
        return getItemViewTypeZ(position);
    }

    protected abstract int getItemViewTypeZ(int position);

    protected static class LoadViewHolder extends RecyclerView.ViewHolder {
        LoadViewHolder(View itemView) {
            super(itemView);
            DataBindingUtil.bind(itemView);
        }
    }

    protected static class EmptyViewHolder extends RecyclerView.ViewHolder {

        EmptyViewHolder(View itemView) {
            super(itemView);
        }
    }
}
